
$(function() {
	
	$("input[id^='code_s']:visible").autocomplete({
		source: function( request, response ) {
			$.ajax({
				url: 'material.php?do=Material.ajax_code',
				dataType: "json",
				data: {
					featureClass: "P",
					style: "full",
					maxRows: 12,
					q: request.term
				},
				success: function( data ) {
					response( $.map( data, function( item ) {
						return {
							label: "[" + item.code + ", " + item.category + "] " + item.name + ' ' + item.standard,
							value: item.code
						}
					}));
				}
			});
		},
		
		minLength: 1
	});
		
	Tip.emp('负责人');
	
	$("select[id^='emp_t_s']:visible").change(function() {
		var label = $("label[id^='label_emp_t']:visible");
		var a = $(this).val();
		if (a == 'emp') {
			label.html("负责人：");
		} else if (a == 'oper') {
			label.html("录入员：");
		} 
	});
});

var Material = {
	validate_add: function () {
		var validator = $("#form_add").validate({
			rules: {
				name: {
					required: true,
					minlength: 2
				},
				unit: "required",
				price: "required"
			},
			messages: {
				name: {
					required: "请输入物料名称",
					minlength: $.validator.format("请输入字数不少于{0}个的名称")
				},
				unit: {
					required: "请输入物料的单位"
				},
				price: {
					required: "请输入物料的单价"
				}
			},
			errorPlacement: function(label, element) {
				label.insertAfter(element);
			},
			success: function(label) {
				label.html("&nbsp;").addClass("checked");
			}
		});

		if (!validator.form()) {
			return false;
		}

		return true;
	},

	dialog_add_: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('material.php?do=Material.audit&item=51', function(json) {
			if (json.status == 1) {
				$('#dialog_add').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},
		
					"添加": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}
		
						var form = $("#form_add")[0];
		
						var body = { };
						body.name = form.name.value;
						body.category = form.category.value;
						body.subclass = form.subclass.value;
		
						body.standard = form.standard.value;
						body.density = form.density.value;
		
						body.unit = form.unit.value;
						body.price = form.price.value;
		
						body.content = form.content.value;
						body.emp = form.emp.value;
		
						$(this).dialog("close");
		
						$.post('material.php?do=Material.addPost', body, function(data) {
							$(result).html(data);
						});
					}
				});
		
				dialog_add(url, title, xy);
			} else {
				_error();
			}
		});
	},

	clean_cache: function () {
		if (!confirm("确认是否清除缓存？")) {
			return false;
		}

		$.get('material.php?do=Material.ajax_category_type', function(data) {
			alert("操作成功！");
		});
	},	

	popup: function (e, p2, id, status) {
		$.cookie('id_', id);

		$.cookie('status_', status);

		p2 = query(p2);
		$.cookie('p2', p2);

		var c1 = 'material.php?do=Material.edit&id=' + id;
		var c2 = 'material.php?do=Material.view&id=' + id;
		var c3 = 'material.php?do=Material.copy&id=' + id;

		var c4 = 'material.php?do=Pic.findPost&material_s=' + id;
		var c5 = 'material.php?do=Pic.add&id=' + id;

		var c6 = 'material.php?do=In.add&id=' + id;

		var a1 = ['编辑信息', 'url', 'Material.dialog_edit_', '650x400'];
		var a2 = ['查看信息', 'url', 'Common.dialog_view_', '600x380'];
		var a3 = ['复制信息', 'url', 'Material.dialog_copy_', '650x400'];

		var a4 = ['图片信息', 'url', 'list'];
		var a5 = ['上传图片', 'url', 'Material.dialog_pic', '600x360'];	

		var a6 = ['直接入库', 'url', 'Material.dialog_in', '700x440'];	

		if (status == 1) {
			var s = c1 + ", " + c2 + ", " + c3 + ", " + c6;

			return this.popup_(e, [a1, a2, a3, a6], s);
		}

		var s = c1 + ", " + c2;

		return this.popup_(e, [a1, a2], s);
	},

	popup_: function (e, a, s) {
		var id = $.cookie('id_');
		
		var ss = 'material.php?do=Material.ss&id=' + id;
		var bb = ['修改状态', 'url', 'Material.dialog_ss', '400x200'];
		
		s += ", " + ss;
		a.push(bb);
		
		return popup(e, a, s);
	},

	/**
	 * 修改状态
	 */
	dialog_ss: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('material.php?do=Material.audit&item=88', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"修改": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}

						var form = $("#form_edit")[0];

						var body = {};
						body.id = form.id.value;
						body.field = form.field.value;
						body.data = form.data.value;
						body.status = form.status.value;

						$(this).dialog("close");

						var url = 'material.php?do=Material.ssPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});
	},

	dialog_edit_: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('material.php?do=Material.ajax_emp', function(json) {
		// $.getJSON('material.php?do=Material.audit&item=51', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"修改": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}

						var form = $("#form_edit")[0];

						var body = {};
						body.id = form.id.value;
						body.code = form.code.value;
						body.name = form.name.value;
						body.category = form.category.value;
						body.subclass = form.subclass.value;

						body.standard = form.standard.value;
						body.density = form.density.value;

						body.unit = form.unit.value;
						body.price = form.price.value;

						body.content = form.content.value;
						body.emp = form.emp.value;
						body.status = form.status.value;

						$(this).dialog("close");

						var url = 'material.php?do=Material.editPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});
	},

	dialog_copy_: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('material.php?do=Material.audit&item=51', function(json) {
			if (json.status == 1) {
				$('#dialog_add').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},
		
					"复制": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}
		
						var form = $("#form_add")[0];
		
						var body = {};
						body.code = form.code.value;
						body.name = form.name.value;
						body.category = form.category.value;
						body.subclass = form.subclass.value;
		
						body.standard = form.standard.value;
						body.density = form.density.value;
		
						body.unit = form.unit.value;
						body.price = form.price.value;
		
						body.content = form.content.value;
						body.emp = form.emp.value;
		
						$(this).dialog("close");
		
						var url = 'material.php?do=Material.addPost';
		
						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});
		
				dialog_add(url, title, xy);
			} else {
				_error();
			}
		});
	},

	dialog_pic: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('material.php?do=Material.audit&item=51', function(json) {
			if (json.status == 1) {
				$('#dialog_add').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"上传": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}

						var form = $("#form_add")[0];

						var body = {};
						var id = form.id.value;
						var filename = form.filename.value;

						var s = "material.php?do=Pic.ajax_upload&id=" + id + "&name=" + filename;
						$('#userfile').uploadifySettings('script', s);

						if ($("#_upload").val() == "1") {
							$(this).dialog("close");
						} else {
							$('#userfile').uploadifyUpload();
						}

						// list_();
					}
				});

				dialog_add(url, title, xy);
			} else {
				_error();
			}
		});
	},

	in_validate: function () {
		var validator = $("#form_add").validate({
			rules: {
				place: {
					required: true,
					minlength: 2
				},
				amount: "required"
			},
			messages: {
				place: {
					required: "请选择或输入仓库信息",
					minlength: $.validator.format("请输入字数不少于{0}个的仓库信息")
				},
				amount: {
					required: "请输入入库的数量"
				}
			},
			errorPlacement: function(label, element) {
				if (/^place/.test(element[0].name)) {
					label.insertAfter("#place_t");
				} else {
					label.insertAfter(element);
				}
			},
			success: function(label) {
				label.html("&nbsp;").addClass("checked");
			}
		});

		if (!validator.form()) {
			return false;
		}

		return true;
	},

	dialog_in: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('material.php?do=Material.audit&item=56', function(json) {
			if (json.status == 1) {
				$('#dialog_add').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"入库": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}

						if (!Material.in_validate()) {
							return false;
						}

						var form = $("#form_add")[0];

						var body = {};
						body.material = form.material.value;
						
						body.emp = form.emp.value;
						body.place = form.place.value;
						
						body.price = form.price.value;
						body.type = form.type.value;
						
						body.name = form.name.value;
						body.standard = _standard(form);
						body.price2 = form.price2.value;
						body.unit2 = form.unit2.value;
						body.density = form.density.value;

						body.unit = form.unit.value;
						body.amount = form.amount.value;
						
						body.date = form.date.value;
						body.content = form.content.value;
						
						price = parseFloat(body.price);
						if (price <= 0) {
							alert("单价不能小于和等于零，请重新核对再入库！");
							return false;
						}

						$(this).dialog("close");

						var url = 'material.php?do=In.addPost';

						$.post(url, body, function(data) {
							alert("操作成功！");
						});
					}
				});

				dialog_add(url, title, xy);
			} else {
				_error();
			}
		});
	},

	cookie_cart: function () {
		var json = $.cookie('cart');
		if (json != null && json.length > 0) {
			return $.evalJSON(json);
		}

		return new Array();
	},

	cart_empty: function () {
		if (!confirm("确认是否清空？")) {
			return false;
		}

		$.cookie('cart', null);

		$("#cart").text("采购单有[0]条明细");
	},

	cart_add: function (id) {
		var cart = this.cookie_cart();

		for (var i = 0; i < cart.length; i++) {
			if (cart[i][0] == id) {
				alert("此信息在采购单已存在，请不要重复添加！");
	            return false;
	        }
	    }

		var mm = [id, null, null];
		cart.push(mm);

		$.cookie('cart', $.toJSON(cart));
		alert("已成功添加至采购单！");

		if (cart != null && cart.length > 0) {
			$("#cart").text("采购单有[" + cart.length + "]条明细");
		}
	},

	cart_validate: function () {
		var validator = $("#form_add").validate({
			rules: {
				name: {
					required: true,
					minlength: 2
				},
				date: "required"
			},
			messages: {
				name: {
					required: "请输入采购单标题",
					minlength: $.validator.format("请输入字数不少于{0}个的标题")
				},
				date: {
					required: "请输入预计交货日期"
				}
			},
			errorPlacement: function(label, element) {
				label.insertAfter(element);
			},
			success: function(label) {
				label.html("&nbsp;").addClass("checked");
			}
		});

		if (!validator.form()) {
			return false;
		}

		return true;
	},

	dialog_cart_my: function () {
		var json = $.cookie('cart');
		if (json == null) {
			alert("采购单为空，请选择您要采购的信息。");
			return false;
		}

		$('#dialog_add').dialog('option', 'buttons', {

			"提交申请": function() {
				if (!confirm("确认是否提交采购申请？")) {
					return false;
				}

				if (!Material.cart_validate()) {
					return false;
				}

				var form = $("#form_add")[0];

				var body = {};
				
				body.name = form.name.value;
				body.date = form.date.value;
				body.address = form.address.value;

				body.content = form.content.value;

				$(this).dialog("close");

				$.post('material.php?do=Purchase.addPost', body, function(data) {
					$.cookie('cart', null);

					$("#cart").text("采购单有[0]条明细");

					alert("操作成功！");
				});
			},

			"继续选购": function() {
				$(this).dialog("close");
			}
		});

		var url = "material.php?do=Purchase.add";

		dialog_add(url, '申请采购', '800x500');
	},
	
	popup_all: function (e, p2) {
		p2 = query(p2);
		$.cookie('p2', p2);

		var c1 = 'material.php?do=Material.delete' + p2;	
		var c9 = 'material.php?do=Material.xls' + p2;	
			
		var a1 = ['删除信息', 'url', 'Common.list_delete'];				
		var a9 = ['导出Excel', 'url', 'jswin', '1024x768'];

		var s = c1 + ", " + c9;

		return popup(e, [a1, a9], s);
	}
};
